package com.qiguliuxing.dts.core.config;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ExecutorConfiguration {
	private final Log logger = LogFactory.getLog(ExecutorConfiguration.class);

    @Bean(name="messageTrunktaskExecutor")
    public AsyncTaskExecutor messageTrunktaskExecutor() {
		logger.info("Configuration ThreadPoolTaskExecutor");
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		// 设置核心线程数
        executor.setCorePoolSize(300);
        // 设置最大线程数
        executor.setMaxPoolSize(300);
        // 设置队列容量
        executor.setQueueCapacity(2000);
        // 设置线程活跃时间（秒）
        executor.setKeepAliveSeconds(30000);
        // 设置默认线程名称
        executor.setThreadNamePrefix("seckill-");
        // 设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
	}
}
